package com.khshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khshop.pojo.Type;
import com.khshop.mapper.TypeMapper;
import com.khshop.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-13
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired(required = false)
    TypeMapper typeMapper;

    @Override
    public IPage<Type> search(Map<String, Object> map, Integer page, Integer limit) {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        Page<Type> iPage = new Page<Type>(page, limit);
        for (String key : map.keySet()) {
            queryWrapper = queryWrapper.like(key, map.get(key));
        }
        return typeMapper.selectPage(iPage, queryWrapper);
    }

    @Override
    public boolean add(Type type) {
        return typeMapper.insert(type)>0;
    }

    @Override
    public boolean update(Type type) {
        return typeMapper.updateById(type)>0;
    }

    @Override
    public List<Type> findTypes(int parentid) {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentid",parentid);
        return typeMapper.selectList(queryWrapper);
    }

    @Override
    public boolean shiftMove(Long []oldTypeId, Long parentId) {
        Type type = new Type();
        type.setParentid(parentId);
        QueryWrapper<Type> queryWrapper = new QueryWrapper<Type>();
        queryWrapper.in("id",  Arrays.asList(oldTypeId));
        return typeMapper.update(type,queryWrapper)>0;
    }

    @Override
    public boolean delTypes(Long[] typeId) {
        return typeMapper.deleteById(typeId)>0;
    }
}
